<template>
  <div class="list-table">
    <el-form ref="form" label-width="120px" class="dataForm" style="left: 20px;right:5px;">
      <!-- 基金收益列表 -->
      <div class="area-header" style="margin-top: 10px;">
        <span class="area-header-title">{{ $t('SPV基金收益列表') }}</span>
      </div>
      <el-table ref="checkTable" v-loading="loading" :data="fundIncomeList" tooltip-effect="dark" border @selection-change="getCheckedData">
        <el-table-column
          v-model="checkedChange"
          type="selection"
          width="55"
        />
        <el-table-column v-if="dataSource === 'deal'" :label="$t('回款项目')" align="center" prop="dealName" />
        <el-table-column :label="$t('回款时间')" align="center" prop="distributionDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.distributionDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('本金') + $t('金额单位')" align="center" prop="amountPrincipal">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.amountPrincipal, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('收益') + $t('金额单位')" align="center" prop="amountIncome">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.amountIncome, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('分红') + $t('金额单位')" align="center" prop="amountBonus">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.amountBonus, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('其他') + $t('金额单位')" align="center" prop="amountOthers">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.amountOthers, 'w') }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <el-button @click="cancel">{{ $t("取消") }}</el-button>
      <el-button type="primary" @click="confirmChecked">{{ $t('确认') }}</el-button>
    </div>
  </div>
</template>

<script>
  import { listFundIncome } from '@/api/fund/income/fundIncome'

export default {
  data() {
    return {
      // 变更历史遮罩层
      loading: true,
      // 对比遮罩层
      loadingVS: false,
      // 查询参数
      queryParams: {
        fundId: this.$route.query.id,
        dataSource: ''
      },
      // 基金收益表格数据
      fundIncomeList: [],
      // 区分是需要项目回款还是手动添加的收益信息
      dataSource: 'deal',
      // 选中的ID
      checkedChange: [],
      // 选中的对象
      checkedObj: []
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询股东信息列表 */
    getList() {
      this.loading = true
      listFundIncome(this.queryParams).then(response => {
        this.fundIncomeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 根据选中的ID，展示对比信息
    getCheckedData(val) {
      this.checkedObj = []
      if (val !== null && val !== undefined) {
        val.forEach(item => {
          var row = {
            id: item.id,
            // 回款项目
            dealName: item.dealName,
            // 回款时间
            distributionDate: item.distributionDate,
            // 本金
            amountPrincipal: item.amountPrincipal,
            // 收益
            amountIncome: item.amountIncome,
            // 分红
            amountBonus: item.amountBonus,
            // 其他
            amountOthers: item.amountOthers
          }
          this.checkedObj.push({ fundIncome: row })
        })
      }
    },
    // 点击确认，将选中的收益数据回显到基金分配页面
    confirmChecked() {
      if (this.checkedObj === [] || this.checkedObj.length === 0) {
        this.msgError(this.$t('请选择收益！'))
        return
      }
      this.$parent.$parent.formOpen = false

      this.checkedObj.forEach(item => {
        if (this.dataSource === 'deal') {
          this.$parent.$parent.form.dealIncomeList.push(item)
        } else {
          this.$parent.$parent.form.otherIncomeList.push(item)
        }
      })
    },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
    }
  }
}
</script>
